#!/usr/local/env python3
'''
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
'''
import requests,re

UrlList = []
# UrlList1 = {
# #     "http://www.apicloud.com":"200",
# #     "http://www.apicloud.com/mam/getV2AppStatisticDataByAppIdAndDate":"400",
# #     "http://www.apicloud.com/mam/getV2VersionStatisticDataByAppIdAndDate":"400",
# #     "http://www.apicloud.com/mam/getV2GeoStatisticDataByAppIdAndVersion":"400",
# #     "http://www.apicloud.com/mam/getV2DeviceStatisticDataByAppIdAndDate":"400",
# #     "http://www.apicloud.com/mam/getV2ExcepStatisticDataByAppIdAndDate":"400",
# #     "http://www.apicloud.com/getV2AppStatisticDataByAppIdAndDate":"200",
# #     "http://www.apicloud.com/getV2VersionStatisticDataByAppIdAndDate":"200",
# #     "http://www.apicloud.com/getV2GeoStatisticDataByAppIdAndVersion":"200",
# #     "http://www.apicloud.com/getV2DeviceStatisticDataByAppIdAndDate":"200",
# #     "http://www.apicloud.com/getV2ExcepStatisticDataByAppIdAndDate":"200",
#
# #     "http://www.apicloud.com/mam/addVer":"400",
# #     "http://www.apicloud.com/mam/changeVerStatus":"400",
# #     "http://www.apicloud.com/mam/delVer":"400",
#     "http://www.apicloud.com/getPKGState":"400"
# }
filepost = 'post.txt'
with open(filepost,'r') as f:
    for line in f.readlines():
        line = line.strip('\n').strip().strip(',') #去掉带有换行符、空格和逗号
        if line == '\n' or len(line) == 0 or re.findall('//',line): #匹配单行字符串是换行的、长度为0的和以‘//’开头的，然后pass
            pass
        else:
            UrlList.append(line)


# def check_api(UrlList):
#     '''
#     检测url返回状态
#     :param urllist: 传入url字典
#     :return:
#     '''
#     try:
#         for key,value in UrlList.items():
#             print("get-status-code:%s" % requests.get(key).status_code)
#             if requests.get(key).status_code == int(value):
#                 print("接口\"%s\"测试通过！" % key)
#             else:
#                 print("接口\"%s\"测试未通过！当前状态码为：%s，正常返回状态码：%s" % (key,requests.get(key).status_code,value))
#                 print("当前URL：%s" % requests.get(key).url)
#     except Exception as e:
#         print("程序异常:",e)
#     finally:
#         print("测试完毕!")
def check_api(UrlList):
    '''
    检测url返回状态
    :param urllist: 传入url列表
    :return:
    '''
    url_bak = []
    try:
        for urlline in UrlList:
            status_code = requests.get('http://www.apicloud.com' + urlline).status_code
            # print("get-status-code:%s" % status_code)
            if status_code == 200:
                url_now = requests.get('http://www.apicloud.com' + urlline).url
                if url_now == 'http://www.apicloud.com/signin':
                    print("当前URL：%s" % url_now)
                    print("接口\"%s\"测试通过！" % urlline)
                else:
                    print("当前URL：%s" % url_now)
                    print("接口\"%s\"测试未通过！当前状态码为：%s" % (urlline,status_code))
                    url_bak.append(urlline)
            elif status_code == 400:
                print("接口\"%s\"测试通过！" %  urlline)
            else:
                print("接口\"%s\"测试未通过！当前状态码为：%s" % (urlline,status_code))
                url_bak.append(urlline)
    except Exception as e:
        print("程序异常:",e)
    finally:
        print("测试完毕!")
        print(url_bak,len(url_bak))

if __name__ == "__main__":
    check_api(UrlList)

